define(['./../initial', './viewMode', './symbolFactory', './chartsFactory', './zoomMap'], function() {
    var app = angular.module('App.home');
    app.factory('renderPointsOnMap', ['viewMode', 'mapInstance', 'symbolFactory', '$timeout', '$rootScope', 'chartsFactory', '$q', 'zoomMap',
        function(viewMode, mapInstance, symbolFactory, $timeout, $rootScope, chartsFactory, $q, zoomMap) {
            return function(autoSurveyPoint, autoSurvey) {
                var defer = $q.defer();
                var prjItem = $rootScope.curPrj;
                //step 1 clear existed markers
                var map = mapInstance.getMap();
                if (map.viewState !== 'active') return;
                map.clearPointsLayer(); //清除当前加载的点
                //step 2 get location points need to be rendered,and find their center
                var targets = null,
                    center = null;
                if (autoSurvey && autoSurveyPoint) {
                    center = { //find center
                        x: autoSurveyPoint.geometry[1],
                        y: autoSurveyPoint.geometry[0]
                    };
                    targets = [autoSurveyPoint];
                } else {
                    var lpts = prjItem.findLocationPoints(viewMode.monitorType, viewMode.location);
                    targets = lpts.points;
                    //看看是否配置好了环号?

                    if (!lpts.isRingNumberConfig) {
                        defer.resolve(null);
                        return defer.promise;
                    }
                    var first = null;
                    if (targets.length < 4) {
                        first = targets[0].geometry;
                        center = L.point(first[1], first[0]);
                    } else {
                        first = targets[3].geometry;
                        var last = targets[targets.length - 3].geometry;
                        center = L.bounds([first[1], first[0]], [last[1], last[0]]).getCenter(); //find center
                    }
                }

                var offset = map.unproject([0, 200], 1);
                //step 3 render these points on map
                zoomMap([center.x + offset.lat, center.y], 1)
                    .then(function() {
                        targets.forEach(function(pt) {
                            var labelIcon = null;
                            if (pt.alert) { //报警点与否
                                labelIcon = L.divIcon({
                                    'html': '<div id="' + pt.name + '" data-content="' + pt.name + '" data-variation="inverted"></div>',
                                    'className': 'point-marker point-marker-alert map-zoom-1',
                                    'iconSize': {
                                        x: 20,
                                        y: 20
                                    }
                                });
                            } else {
                                labelIcon = L.divIcon({
                                    'html': '<div id="' + pt.name + '" data-content="' + pt.name + '" data-variation="inverted"></div>',
                                    'className': 'point-marker point-marker-normal map-zoom-1',
                                    'iconSize': {
                                        x: 20,
                                        y: 20
                                    }
                                });
                            }

                            var marker = L.marker([pt.geometry[1], pt.geometry[0]], {
                                icon: labelIcon,
                                riseOnHover: true
                            });
                            map.curPointMarksLayer.addLayer(marker);
                            marker.name = pt.name;
                            if (!autoSurvey) { //如果非自动化观测
                                marker.on('mouseover', (function(name) {
                                    return function(e) {
                                        var offSet = !$rootScope.smallScreen && $('#leftBar').sidebar('is open') ? $('#leftBar').width() : 0;
                                        // debugger;
                                        $(name).popup({
                                            offset: -offSet
                                        }).popup('show');
                                        e.originalEvent.preventDefault();
                                        e.originalEvent.stopPropagation();
                                    };
                                })('#' + pt.name));
                                marker.on('mouseout', (function(name) {
                                    return function(e) {
                                        $(name).popup('hide');
                                        e.originalEvent.preventDefault();
                                        e.originalEvent.stopPropagation();
                                    };
                                })('#' + pt.name));
                                marker.on('click', (function(name) {
                                    return function() {
                                        chartsFactory.openSinglePointModal(name);
                                    };
                                })(pt.name));
                            } else {
                                marker.bindPopup('<span class="auto-point-popup-span">' + pt.name + "(" + pt.ringNumber + "环)</span>", {
                                    closeButton: false,
                                    offset: new L.Point(0, 3)
                                }).openPopup();
                            }
                        });
                        defer.resolve(targets);
                    });
                return defer.promise;
            };
        }
    ]);
});
